﻿using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public delegate void ProcessHeThong(int percent);
    public class HeThongManager
    {
        public ProcessHeThong prcs;

        public ActionResult SaoLuu(string path)
        {
            ActionResult rs = new ActionResult();
            try
            {
                ServerConnection srvcnn = new ServerConnection(ConfigurationManager.AppSettings["ServerName"]);
                srvcnn.LoginSecure = true;

                Server sv = new Server(srvcnn);

                string destPath = Path.Combine(sv.BackupDirectory, Path.GetFileName(path));

                Backup backup = new Backup();
                backup.Initialize = true;
                backup.Action = BackupActionType.Database;
                backup.Database = ConfigurationManager.AppSettings["DatabaseName"];
                backup.Devices.AddDevice(destPath, DeviceType.File);

                backup.PercentCompleteNotification = 10;
                backup.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);

                backup.SqlBackup(sv);

                File.Copy(destPath, path, true);
                File.Delete(destPath);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult PhucHoi(string path)
        {
            ActionResult rs = new ActionResult();
            try
            {
                ServerConnection srvcnn = new ServerConnection(ConfigurationManager.AppSettings["ServerName"]);
                srvcnn.LoginSecure = true;

                Server sv = new Server(srvcnn);

                string destPath = Path.Combine(sv.BackupDirectory, Path.GetFileName(path));
                File.Copy(path, destPath, true);

                using (var db = new ThuVienDbContext())
                {
                    var cmd = String.Format("alter database {0} set offline with rollback immediate", ConfigurationManager.AppSettings["DatabaseName"]);
                    db.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, cmd);
                }

                Restore res = new Restore();
                res.Database = ConfigurationManager.AppSettings["DatabaseName"];
                res.Action = RestoreActionType.Database;
                res.Devices.AddDevice(destPath, DeviceType.File);
                res.ReplaceDatabase = true;

                res.PercentCompleteNotification = 10;
                res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);

                res.SqlRestore(sv);

                File.Delete(destPath);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                rs.AddException(ex);
            }
            return rs;
        }

        private void ProgressEventHandler(object sender, PercentCompleteEventArgs e)
        {
            if (prcs != null)
                prcs(e.Percent);
        }
    }
}
